package xu.htgl.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result {

    // 状态码
    private Integer code;
    // 响应描述
    private String message;
    // 响应数据
    private Object data;

    private Result(ResultEnum resultEnum, Object data) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.getMsg();
        this.data = data;
    }

    private Result(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.getMsg();
    }


    public static Result ok() {
        return new Result(ResultEnum.SUCCESS);
    }

    public static Result ok(Object data) {
        return new Result(ResultEnum.SUCCESS, data);
    }

    public static Result ok(String msg, Object data) {
        return new Result(ResultEnum.SUCCESS, data).setMessage(msg);
    }

    public static Result ok(Integer code, String msg, Object data) {
        return new Result(code, msg, data);
    }

    public static Result failed() {
        return new Result(ResultEnum.FAILED);
    }

    public static Result failed(ResultEnum resultEnum) {
        return new Result(ResultEnum.TOKEN_EXPIRED);
    }

    public static Result failed(Object data) {
        return new Result(ResultEnum.FAILED, data);
    }

    public static Result failed(String msg) {
        return new Result(ResultEnum.FAILED).setMessage(msg);
    }

    public static Result failed(String msg, Object data) {
        return new Result(ResultEnum.FAILED, data).setMessage(msg);
    }

    public static Result failed(Integer code, String msg, Object data) {
        return new Result(code, msg, data);
    }
}
